package com.xrit.sunnyday.controller;

import com.xrit.sunnyday.constant.RedisMessageConstant;
import com.xrit.sunnyday.entity.Result;
import com.xrit.sunnyday.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * 类名称: validateCodeController 短信发送类 并短信保存在redis中
 * 类描述:TODO
 * 创建人: czb
 * 创建时间:2022/6/29 下午 01:46
 * Version 1.0.0
 */
@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {
    @Autowired
    private JedisPool jedisPool;

    @PostMapping("/send4Order")
    public Result send4Order(String telephone) {
        Jedis resource = jedisPool.getResource();
        //如果验证码不为空
        if (!StringUtils.isEmpty(resource.get(RedisMessageConstant.SENDTYPE_ORDER +"_"+telephone))) {
            return new Result(false, "验证码已发送,请查看短信");
        }
        //调用工具类得到验证码
        Integer code = ValidateCodeUtils.generateValidateCode(6);
        //存入redis
        resource.setex(RedisMessageConstant.SENDTYPE_ORDER +"_"+telephone, 300, code + "");
        return new Result(true, "请输入验证码");
    }
    @PostMapping("/send4Login")
    public Result send4Login(String telephone){
        Jedis resource = jedisPool.getResource();
        //如果验证码不为空
        if (!StringUtils.isEmpty(resource.get(RedisMessageConstant.SENDTYPE_LOGIN +"_"+telephone))) {
            return new Result(false, "验证码已发送,请查看短信");
        }
        //调用工具类得到验证码
        Integer code = ValidateCodeUtils.generateValidateCode(6);
        //存入redis
        resource.setex(RedisMessageConstant.SENDTYPE_LOGIN +"_"+telephone, 300, code + "");
        return new Result(true, "请输入验证码");
    }
}
